package cn.iocoder.yudao.module.pickbook.controller.app;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.pickbook.dal.dataobject.classes.PickbookClassDO;
import cn.iocoder.yudao.module.pickbook.service.classinfo.PickbookClassService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "班级 APP API")
@RestController
@RequestMapping("/pickbook/app/class")
@Validated
@Slf4j
public class PickbookAppClassController {

    @Resource
    private PickbookClassService classService;

    @PostMapping("/create")
    @Operation(summary = "创建班级")
    public CommonResult<PickbookClassDO> createClass(
            @RequestParam @NotBlank(message = "班级名称不能为空") String name,
            @RequestParam(required = false) String description,
            @RequestParam @NotNull(message = "教师ID不能为空") Long teacherId) {
        
        PickbookClassDO classInfo = classService.createClass(name, description, teacherId);
        return success(classInfo);
    }

    @PostMapping("/join")
    @Operation(summary = "加入班级")
    public CommonResult<PickbookClassDO> joinClass(
            @RequestParam @NotBlank(message = "班级口令不能为空") String classCode,
            @RequestParam @NotNull(message = "用户ID不能为空") Long userId) {
        
        PickbookClassDO classInfo = classService.joinClassByCode(classCode, userId);
        return success(classInfo);
    }

    @GetMapping("/list")
    @Operation(summary = "获取用户班级列表")
    public CommonResult<List<PickbookClassDO>> getUserClasses(
            @RequestParam @NotNull(message = "用户ID不能为空") Long userId) {
        
        List<PickbookClassDO> classes = classService.getUserClasses(userId);
        return success(classes);
    }

    @GetMapping("/detail")
    @Operation(summary = "获取班级详情")
    public CommonResult<PickbookClassDO> getClassDetail(
            @RequestParam @NotNull(message = "班级ID不能为空") Long id) {
        
        PickbookClassDO classInfo = classService.getClass(id);
        return success(classInfo);
    }

    @PutMapping("/update")
    @Operation(summary = "更新班级信息")
    public CommonResult<Boolean> updateClass(@RequestBody PickbookClassDO classInfo) {
        classService.updateClass(classInfo);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除班级")
    public CommonResult<Boolean> deleteClass(
            @RequestParam @NotNull(message = "班级ID不能为空") Long id) {
        
        classService.deleteClass(id);
        return success(true);
    }

    @GetMapping("/generate-code")
    @Operation(summary = "生成班级口令")
    public CommonResult<String> generateClassCode() {
        String classCode = classService.generateClassCode();
        return success(classCode);
    }

} 